package com.tarzan.edu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tarzan.common.core.base.BaseResponse;
import com.tarzan.common.core.exception.BusinessException;
import com.tarzan.edu.entity.EduTeacher;
import com.tarzan.edu.entity.request.EduTeacherReq;
import com.tarzan.edu.entity.response.EduTeacherResp;
import com.tarzan.edu.service.IEduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liujinshan
 * @title: EduTeacherController
 * @projectName com.tarzan.edu.controller
 * @description: TODO 讲师控制层
 * @date 2021/5/21 15:39
 */
@RestController
@RequestMapping("edu/teacher")
public class EduTeacherController {

    @Autowired
    private IEduTeacherService iEduTeacherService;



    /** @title insert
     * @description TODO 新增讲师
     * @param eduTeacher
     * @return com.tarzan.common.core.base.BaseResponse
     * @throws
     * @author liujinshan
     * @data 2021/5/21 15:52
     */
    @PostMapping(value = "insert",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse insert(@RequestBody EduTeacher eduTeacher){
        try{
            iEduTeacherService.insert(eduTeacher);
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }

    /** @title update
     * @description TODO 修改讲师信息
     * @param eduTeacher
     * @return com.tarzan.common.core.base.BaseResponse
     * @throws
     * @author liujinshan
     * @data 2021/5/21 15:59
     */
    @PutMapping(value = "update",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse update(@RequestBody EduTeacher eduTeacher){
        try{
            iEduTeacherService.update(eduTeacher);
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }

    /** @title delete
     * @description TODO 删除讲师信息
     * @param id
     * @return com.tarzan.common.core.base.BaseResponse
     * @throws
     * @author liujinshan
     * @data 2021/5/21 16:01
     */
    @DeleteMapping(value = "delete/{id}")
    public BaseResponse delete(@PathVariable String id){
        try{
            iEduTeacherService.delete(id);
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }


    /**
      * @description 获取讲师信息列表并分页
      * @author liujinshan
      * param eduTeacherReq
      * @return com.tarzan.common.core.base.BaseResponse
      * @throws
      * @date 2021-5-23 14:43
      */
    @PostMapping(value = "findTeacherPageInfo")
    public BaseResponse<IPage<EduTeacherResp>> findTeacherPageInfo(@RequestBody EduTeacherReq eduTeacherReq){
        BaseResponse<IPage<EduTeacherResp>> response = new BaseResponse<>();
        try{
            response = BaseResponse.ok(iEduTeacherService.findTeacherPageInfo(eduTeacherReq));
        } catch (BusinessException e){
            response.setErrCode(e.getCode());
            response.setErrMsg(e.getMsg());
        } catch (Exception e){
            response.setErrCode("500");
            response.setErrMsg(e.getMessage());
        }
        return response;
    }

    /**
      * @description 根据id查询讲师相细信息
      * @author liujinshan
      * param id
      * @return
      * @throws
      * @date 2021-5-23 14:51
      */
    @GetMapping(value = "findTeacherById/{id}")
    public BaseResponse<EduTeacherResp> findTeacherById(@PathVariable String id){
        BaseResponse<EduTeacherResp> response;
        try{
            response = BaseResponse.ok(iEduTeacherService.findTeacherById(id));
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return response;
    }


    /**
     * @title findAllTeacherInfo
     * @description TODO 查询所有讲师信息
     * @param
     * @return com.tarzan.common.core.base.BaseResponse<java.util.List<com.tarzan.edu.entity.response.EduTeacherResp>>
     * @author liujinshan
     * @data 2021/8/13 10:29
     */
    @GetMapping(value = "findAllTeacherInfo")
    public BaseResponse<List<EduTeacherResp>> findAllTeacherInfo(){
        BaseResponse<List<EduTeacherResp>> response;
        try{
            response = BaseResponse.ok(iEduTeacherService.findAllTeacherInfo());
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return response;
    }
}
